package com.binguo.yprecapi;

import com.alibaba.fastjson.JSONObject;
import com.binguo.common.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class YpUtilService {

    private static Logger logger = LoggerFactory.getLogger(YpUtilService.class);

    public RestTemplate restTemplate = new RestTemplate();

    public String recIp;

    public HttpHeaders headers = new HttpHeaders();


    public YpUtilService(){
        headers.add("Content-Type", "application/x-www-form-urlencoded");
    }

    public String predict(String uuid, String shopid, String content, String localRecIp) {
        MultiValueMap postMap = new LinkedMultiValueMap();
        postMap.add("filecontent", content);
        postMap.add("shopid", shopid);
        postMap.add("uuid", uuid);

        HttpEntity requestBody = new HttpEntity(postMap, headers);
        ResponseEntity<String> resp = restTemplate.postForEntity("http://"+localRecIp+"/predict", requestBody, String.class);
        String result = resp.getBody();

        logger.info(result);

        return result;
    }

}
